package cn.edu.jnu.x2022180085;
public class BowlingGame {
    int pins[]=new int[21];
    int currentPinIndex=0;
    public void roll(int pin) {
        pins[currentPinIndex++]=pin;
    }

    public int score() {
        int totalScore=0;
        int currentFrameScodeIndex=0;
        for(int currentFrame=0;currentFrame<10;currentFrame++)
        {
            totalScore+=pins[currentFrameScodeIndex];
            totalScore+=pins[currentFrameScodeIndex+1];
            if(isaStrike(currentFrameScodeIndex))
            {
                totalScore+=pins[currentFrameScodeIndex+2];
            }
            else if(isaSpare(currentFrameScodeIndex))
            {
                totalScore+=pins[currentFrameScodeIndex+2];
                currentFrameScodeIndex++;
            }
            else
            {
                currentFrameScodeIndex++;
            }
            currentFrameScodeIndex++;
        }
        return totalScore;
    }
    private boolean isaStrike(int scoreIndex) {
        return 10 == pins[scoreIndex];
    }
    private boolean isaSpare(int scoreIndex) {
        return 10 == pins[scoreIndex] + pins[scoreIndex + 1];
    }
}
